<template>
  <div>
    <el-button type="text" @click="saveUI">新建手术</el-button><br />
    <el-col :span="5">
      <el-input v-model="operationName" placeholder="请输入手术名称"></el-input>
    </el-col>
    <el-button type="info" @click="search">查询</el-button>

    <!--  <el-row class="demo-autocomplete">
      <el-col :span="5">
        <el-autocomplete
          class="inline-input"
          v-model="operationName"
          placeholder="请输入手术名称"
          :trigger-on-focus="false"
          @select="handleSelect"
        ></el-autocomplete>
      </el-col>
    </el-row> -->

    <el-dialog title="手术" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="手术名称" :label-width="formLabelWidth">
          <el-input v-model="form.operationName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手术简介" :label-width="formLabelWidth">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手术价格" :label-width="formLabelWidth">
          <el-input v-model="form.operationPrice" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手术分类" :label-width="formLabelWidth">
          <div class="block">
            <el-cascader
              v-model="form.parentId"
              :options="addtableData"
              :show-all-levels="false"
              :props="{
                expandTrigger: 'hover',
                value: 'operationId',
                label: 'operationName',
                children: 'children',
                emitPath: false,
                checkStrictly:true
              }"
              @change="handleChange"
            ></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitform">确 定</el-button>
      </div>
    </el-dialog>

    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="operationId"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="operationId" label="手术编号" sortable width="180">
      </el-table-column>
      <el-table-column
        prop="operationName"
        label="手术名称"
        sortable
        width="180"
      >
      </el-table-column>
      <el-table-column prop="description" label="手术简介" sortable width="180">
      </el-table-column>
      <el-table-column
        prop="operationPrice"
        label="手术价格"
        sortable
        width="180"
      >
      </el-table-column>
      <el-table-column label="操作" sortable width="180">
        <template slot-scope="scope">
          <el-button type="danger" @click="del(scope.row)">删除</el-button>
          <el-button type="info" @click="update(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  getlist,
  dele,
  update,
  save,
  search,
} from "@/api/operation/operation.js";
export default {
  data() {
    return {
      restaurants: [],
      tableData: [],
      currentPage: 1,
      pageNumber: 1,
      pageSize: 5,
      total: 0,
      form: {},
      dialogFormVisible: false,
      formLabelWidth: "120px",
      operationName: "",
      addtableData: [],
    };
  },
  methods: {
    search() {
      if (this.operationName == "") {
        this.getlist();
        return;
      }
      search({
        operationName: this.operationName,
        pageNumber: this.pageNumber,
        pageSize: this.pageSize,
      }).then((r) => {
        if (r.code == 200) {
          this.tableData = r.data.list;
          this.total = r.data.total;
        }
      });
    },
    saveUI() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    submitform() {
      if (this.form.operationId == null) {
        save(this.form).then((r) => {
          if (r.code == 200) {
            this.$message.success("添加成功");
            this.getlist();
            this.dialogFormVisible = false;
          }
        });
        return;
      }
      update(this.form).then((r) => {
        if (r.code == 200) {
          this.$message.success("修改成功");
          this.getlist();
        } else {
          this.$message.error("修改失败");
        }
        this.dialogFormVisible = false;
      });
    },
    update(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    handleChange(value) {
      console.log(value);
    },
    getlist() {
      getlist({ pageNumber: this.pageNumber, pageSize: this.pageSize }).then(
        (r) => {
          this.tableData = r.data.list;
          this.total = r.data.total;
          this.restaurants = r.data.list;
          this.addtableData = r.data.list;
        }
      );
    },
    del(row) {
      dele({ operationId: row.operationId }).then((r) => {
        if (r.code == 200) {
          this.getlist();
          this.$message.success("删除成功");
        } else {
          this.$message.error("删除失败");
        }
        this.dialogFormVisible = false;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      if (this.operationName == null) {
        this.getlist();
      } else {
        this.search();
      }
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNumber = val;
      if (this.operationName == null) {
        this.getlist();
      } else {
        this.search();
      }
    },
    handleSelect(item) {
      console.log(item);
    },
  },
  mounted() {
    this.getlist();
  },
};
</script>

<style>
</style>